package ming.servlet;

import ming.util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//@WebServlet("/logins")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        String pw = request.getParameter("password");
        //获取列表集合按道理应该就一个 这里我直接找了一个工具类
        List<List<Object>> lists = JdbcUtil.selectSqlToList("select * from user where name=?", name);
        boolean isSucess = false;

        for (List<Object> list : lists) {
            if (list.get(2).equals(pw) && list.get(1).equals(name)) {
                isSucess = true;
                break;
            }
        }

        if (isSucess) {
            request.getSession().setAttribute("useronly", "useronly");
            System.out.println("登录成功");
            request.setAttribute("result", name);
            request.getRequestDispatcher("/WEB-INF/LoginSuccess.jsp").forward(request, response);
        } else {
            System.out.println("登录失败");
            request.setAttribute("result", "登录失败，密码" + pw);
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }

    }


}
